﻿#region Copyrigth and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ucenter
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;

namespace UCenter.ContactService.Email {
    public class Email21CnService : IEmailService {
        #region Fields

        #endregion

        #region Constructors

        #endregion

        #region Properties
        public string EmailSystem {
            get { return "21cn.com"; }
        }

        public string StartUrl {
            get { return "http://passport.21cn.com/maillogin.jsp"; }
        }

        public Encoding DefaultEncoding {
            get { return Encoding.GetEncoding("gb2312"); }
        }
        #endregion

        #region Events

        #endregion

        public MailContractCollection GetContracts(string userName, string pwd) {
            MailContractCollection mc = new MailContractCollection();
            var nvc = new NameValueCollection();
            nvc.Add("LoginName", userName);
            nvc.Add("DomainName", EmailSystem);
            nvc.Add("Template", "extend");
            nvc.Add("UserName", userName);
            nvc.Add("passwd", pwd);
            nvc.Add("NeedMoreSecurity", "on");
            nvc.Add("NeedIpCheck", "on");
            nvc.Add("remUser", "");
            string postContent = WebOpHelper.GetNameValueRowData(nvc, '&');
            string accept = "image/gif, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-silverlight-2-b2, application/x-silverlight, */*";
            string language = "zh-cn";
            string refer = "http://mail.21cn.com/";
            string contentType = "application/x-www-form-urlencoded";
            string returnUrl = StartUrl;
            CookieContainer cc = new CookieContainer();
            string html = WebOpHelper.Post(StartUrl, accept, contentType, refer, language, postContent, cc, DefaultEncoding, out returnUrl);
            string nextUrl = WebOpHelper.Resove(html, "window.location.href=\"", "\"");
            nextUrl = "http://" + new Uri(returnUrl).Host + nextUrl;
            html = WebOpHelper.GetHtml(nextUrl, accept, contentType, refer, language, cc, Encoding.GetEncoding("utf-8"), out returnUrl);
            string url = "http://"+new Uri(returnUrl).Host+"/webmail/addressBookList.do";
            html = WebOpHelper.GetHtml(url, accept, contentType, refer, language, cc, Encoding.GetEncoding("utf-8"), out returnUrl);
            var list = WebOpHelper.ResoveList(html, "<td class=\"mtb1-td2\">", "</td>");
            foreach (string address in list) {
                MailContract m = new MailContract();
                m.Email = address;
                mc.Add(m);
            }
            List<string> list2 = WebOpHelper.ResoveList(html, "target=\"mainFrame\">", "</a>");
            int i = 0;
            foreach (string s in list2) {
                var s1 = s.Trim();
                if (string.IsNullOrEmpty(s1)) {
                    mc[i].Name = mc[i].Email;
                }
                else {
                    mc[i].Name = s1;
                }
                i++;
            }
            return mc;
        }
    }
}
